<?php
/**
 * 订单数据管理
 * PHP version 5
 * @package   Antonia
 * @author    Antonia <471699715@qq.com>
 * @copyright 2016 Antonia.
 */
namespace Admin\Controller;
use Think;
class OrdersController extends AdmincommonController {
    /**
     * 显示商品列表
     * @param HttpRequest $request
     * @return html
     * @author Antonia
     */
    public function index() {
        $filters = array();
        $orders = D("Orders");
        $_GET['p'] = $_POST['p'];
        if(!empty($_POST['createTime'])) {
            $createTime  = strtotime($_POST['createTime']);
            $createTimes = strtotime($_POST['createTimes']);
            if($createTime > $createTimes){
                 $filters['createTime']  = array('between',array($createTimes,$createTime));
            } else {
                $filters['createTime']  = array('between',array($createTime,$createTimes));
            }
        }
        if (!empty($_POST["status"]) && $_POST["status"] != 5) {
            $filters['status'] = intval(remove_xss($_POST['status']));
        }
        if (!empty($_POST["userName"])) {
            $map["userName"] = array("like","%".remove_xss($_POST["userName"])."%");
            $user_model = D("Users")->where($map)->field("id")->select();
            for ($i=0; $i <count($user_model); $i++) { 
                $ids .= $user_model[$i]["id"].",";
            }
            $filters["userId"] = array("in",$ids);
        }
        $count = $orders->where($filters)->count();
        if (!empty($_POST['numPerPage'])){
            $numPerPage = $_POST['numPerPage'];
        } else {
            $numPerPage = 20;
        }
        $Page  =  new \Think\Page($count,$numPerPage);
        $list = $orders->limit($Page->firstRow.','.$Page->listRows)
                    ->relation(true)
                    ->where($filters)
                    ->select();
        $this->assign('list',$list);
        $this->assign('filters', $_POST);
        $this->assign ('totalCount', $count);
        $this->assign ('numPerPage', $Page->listRows);
        $this->assign ('currentPage', !empty($_GET['p'])?$_GET['p']:1);
        $this->display();
    }
    /**
     * 发货
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function shipments() {
        if (!empty($_GET["id"])) {
            $filters = array();
            $Datas = D("Datas");
            $_GET['p'] = $_POST['p'];
            $filters["dataType"] = "EXPRESS";
            $filters["status"] = "0";
            $list = $Datas->where($filters)->select();
            $this->assign("id",$_GET["id"]);
            $this->assign("list",$list);
            $this->display();
        }else if(!empty($_POST["id"])){
            $Orders = D("Orders");
            if ($vo = $Orders->create()) {
                $rs = $Orders->save($vo);
                if ($rs !== false) {
                    $re['statusCode'] = "200";
                    $re['message'] = "保存快递信息成功!";
                    $re['navTabId'] = "Orders";
                    $re['callbackType'] = "closeCurrent";
                } else {
                    $re['statusCode'] = "300";
                    $re['message'] = "保存快递信息失败!";
                }
            } else {
                    $re['statusCode'] = "300";
                    $re['message'] = $Orders->getError();
            }
            echo json_encode($re);
        }
    }
    /**
     * 删除订单
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delete() {
        $re = array();
        if (!empty($_GET['id'])) {
            $Orders = D("Orders");
            $data["id"] = $_GET["id"];
            $data["status"] = 4;
            $result = $Orders->save($data);
            if (false !== $result) {
                $re['statusCode'] = "200";
                $re['message'] = "删除成功!";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "删除失败，请重试!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = "该记录不存在！";
        }
        echo json_encode($re);
    }

    /**
     * 删除选定内容
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delAll() {
        $Orders = D("Orders");
        $re = array();
        $map['id']  = array('in', explode(',', $_POST["ids"]));
        $data["status"] = 4;
        $result  = $Orders->where($map)->save($data);
        if($result) {
            $re['statusCode'] = "200";
            $re['message'] = "删除成功!";
        } else {
            $re['statusCode'] = "300";
            $re['message'] = "删除失败，请重试!";
        }
        echo json_encode($re);
    }

    /**
     * 显示编辑页面
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function edit() {
        $id = remove_xss($_GET['id']);
        if (!empty($id)) {
            $Orders     = D("Orders");
            $Orders_info = $Orders->find($id);
            if ($Orders_info) {
                $filters = array();
                $convert = D("Datas");
                $filters["dataType"] = "EXPRESS";
                $filters["status"] = "0";
                $list = $convert->where($filters)->select();
                $this->assign("list",$list);
                $this->assign('info', $Orders_info);
                $this->display('info');
            } else {
                $re['statusCode'] = "300";
                $re['navTabId'] = "Productsindex";
                $re['message'] = "该订单信息不存在！";
                $re['callbackType'] = "closeCurrent";
                echo json_encode($re);
            }
        } else {
                $re['statusCode'] = "300";
                $re['navTabId'] = "Productsindex";
                $re['message'] = "该订单信息不存在！";
                echo json_encode($re);
        }
    }

    /**
     * 处理更新数据
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function update() {
        $Orders = D("Orders");
        $re = array();
        if ($vo = $Orders->create()) {
            $list = $Orders->save($vo);
            if ($list !== false) {
                $re['statusCode'] = "200";
                $re['message'] = "商品信息修改成功!";
                $re['navTabId'] = "Ordersindex";
                $re['callbackType'] = "closeCurrent";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "没有更新任何数据!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = $Orders->getError();
        }
        echo json_encode($re);
    }



}
?>